<template>
    <div class="small-components">
        <div class="search-nav">
            <ul class="search-nav-list flex-x">
                <li class="search-nav-item flex-x" :class="{ 'active': activeNav == item.name }" v-for="item in navList"
                    :key="item.name" @click="changeNav(item.name)">
                    <img :src="item.icon" mr4 style="width:16px;height:16px">
                    {{ item.name }}
                </li>
            </ul>
        </div>
        <div class="flex-between" mr40 mb10 v-show="activeNav == '探索'">
            <ul class="tabs-title flex-x-end">
                <li class="tabs-title-item" :class="{'active': tabTitle == '今日推荐' }" @click="changeTitle('今日推荐')">今日推荐</li>
                <li class="tabs-title-item" :class="{ 'active': tabTitle == '最近更新' }" @click="changeTitle('最近更新')">最近更新</li>
                <li class="tabs-title-item" :class="{ 'active': tabTitle == '最受欢迎' }" @click="changeTitle('最受欢迎')">最受欢迎</li>
            </ul>
            <span fs-12 class="flex-center pointer">
                <el-icon size="18px" color="gba(var(--alpha-color), .5)" mr4>
                    <Refresh />
                </el-icon>
                换一批
            </span>
        </div>
        <div class="component-grid">
            <div class="component-card" v-for="item in componentList" :key="item._id">
                <div class="component-head">
                    <p fs-16 class="name">{{ item.name }}</p>
                    <p fs-13 class="introduct text-clip2" :title="item.description">{{ item.description }}</p>
                </div>
                <div class="component-carousel">
                    <iconComponent v-if="item.type == 'icon' && item.insetType == 'iframe'" @add="handleAddComponent(item)"
                    :imgUrl="item.src" :bgColor="item.backgroundColor" :iframeUrl="item.iframeUrl" :ref="(el: ComponentPublicInstance) => setComponentRefs(el, item)"></iconComponent>
                    <comTemplate v-if="item.type == 'component'" :component="globalStore.componentList[item.component]" :ref="(el: ComponentPublicInstance) => setComponentRefs(el, item)" @add="handleAddComponent(item)"></comTemplate>
                </div>
                <div class="component-footer flex-between">
                    <div fs-14>
                        <i class="iconfont icon-Forme" style="font-size: 18px;color:rgb(255, 122, 6)"></i>
                        {{ (item.downloadCount / 10000).toFixed(2) }}万
                    </div>
                    <el-button size="small" type="primary" style="border-radius:12px" @click="handleAddComponent(item)">添加</el-button>
                </div>
            </div>
        </div>
    </div>
</template>

<script setup lang="ts">
import { Refresh } from '@element-plus/icons-vue'
import { componentList } from '@/data/data'
import { useGlobalStore } from '@/store/module/global'
import { AppListType } from '@/store/interface/global';

const activeNav = ref('探索')
const tabTitle = ref('今日推荐')

const globalStore = useGlobalStore()

const navList = [
    { name: '探索', icon: 'https://files.codelife.cc/itab/widget-category/discovery.svg' },
    { name: '全部', icon: 'https://files.codelife.cc/itab/widget-category/widget.svg' },
    { name: '效率', icon: 'https://files.codelife.cc/itab/widget-category/efficiency.svg' },
    { name: '工具', icon: 'https://files.codelife.cc/itab/widget-category/tool.svg' },
    { name: '开发', icon: 'https://files.codelife.cc/itab/widget-category/develop.svg' },
    { name: '设计', icon: 'https://files.codelife.cc/itab/widget-category/design.svg' },
    { name: '创意', icon: 'https://files.codelife.cc/itab/widget-category/ideas.svg' },
    { name: '娱乐', icon: 'https://files.codelife.cc/itab/widget-category/game.svg' },
    { name: '其他', icon: 'https://files.codelife.cc/itab/widget-category/others.svg' },
]

const changeNav = (name: string) => {
    activeNav.value = name
}

const changeTitle = (title:string) => {
    tabTitle.value = title
}

//动态设置组件ref
const refList = new Map()
const setComponentRefs = (el: ComponentPublicInstance,item:any) => {
    if(refList.has(`ref_${item._id}`))
        return
    refList.set(`ref_${item._id}`,el)
}

//添加
const handleAddComponent = (item:any) => {
    const size = refList.has(`ref_${item._id}`) ?refList.get(`ref_${item._id}`).getIconSize():'1x1'
    const data = {
        id:uuid(2),
        name:item.name,
        iconImg:'',
        bgColor:"",
        type:'component',
        iframeUrl:'',
        size,
        component:item.component
    }
    if(item.insetType == 'iframe'){
        data.iconImg = item.src
        data.bgColor = item.backgroundColor
        data.type = 'iframe'
        data.iframeUrl = item.iframeUrl
    }
    globalStore.addGroupAppList(data as AppListType)
}
</script>

<style lang="scss">
.small-components {
    height: calc(100% - 40px);
    display: flex;
    flex-direction: column;

    .search-nav {
        margin: 6px 0;

        .search-nav-list {
            list-style: none;

            .search-nav-item {
                height: 22px;
                line-height: 20px;
                padding: 0 6px;
                margin-right: 10px;
                background-color: var(--bg-card);
                color: var(--d-main);
                border-radius: 11px;
                cursor: pointer;
                border: 1px solid rgba(var(--alpha-color), .06);
                font-size: 12px;
                transition: all .2s;

                &.active {
                    background-color: var(--primary-color);
                    color: #fff;
                    transform: scale(1.03);
                }
            }
        }
    }

    .tabs-title {
        font-size: 14px;
        list-style: none;
        color: rgba(var(--alpha-color), .6);
        height: 22px;


        .tabs-title-item {
            margin-right: 20px;
            line-height: 18px;
            transition: all .2s ease;
            cursor: pointer;
            &.active{
                line-height: 22px;
                font-size: 18px;
                font-weight: bold;
                color: rgb(var(--alpha-color));
            }
        }
    }

    .component-grid{
        overflow-y: auto;
        display: grid;
        grid-template-columns: repeat(auto-fill,minmax(380px,1fr));
        gap:16px 16px;
        grid-auto-flow: dense;

        .component-card{
            border-radius: 12px;
            padding: 10px;
            background-color: var(--bg-card);

            .component-head{
                text-align: center;
                .name{
                    margin: 4px 0px 6px;
                    color: var(--d-main);
                    height: 18px;
                    line-height: 18px;
                }
                .introduct{
                    color: rgba(var(--alpha-color), .5);
                    height: 27px;
                    line-height: 1;
                }
            }

            .component-carousel{
                height: 190px;

            }

            .component-footer{
                height: 24px;
                line-height: 24px;
                margin-top: 16px;
                color: var(--d-main);
            }
        }
    }
}</style>